<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
            >
            <el-table-column label="订单号" align="center" prop="order_id" width="100px"/>
            <el-table-column label="用户" align="center" prop="user.nickname" width="100px"/>
            <el-table-column label="用户类型" align="center" prop="user_type" width="120px">
              <template #default="scope">
                <el-tag
                  :type="getUserTypeTag(scope.row.user_type)"
                >
                  {{ getUserTypeLabel(scope.row.user_type) }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="金额" align="center" prop="amount" width="120px">
              <template #default="scope">
                <span :class="scope.row.amount >= 0 ? 'income' : 'expense'">
                  {{ scope.row.amount }}
                </span>
              </template>
            </el-table-column>
            <el-table-column label="类型" align="center" prop="type" width="100px">
              <template #default="scope">
                <el-tag :type="scope.row.type === 1 ? 'danger' : 'success'">
                  {{ scope.row.type === 1 ? '提现' : '分销收入' }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="type" width="100px">
              <template #default="scope">
                <el-tag v-if="scope.row.status === 0" type="primary">
                  处理中
                </el-tag>
                <el-tag v-if="scope.row.status === 1" type="success">
                  成功
                </el-tag>
                <el-tag v-if="scope.row.status === -1" type="danger">
                  失败
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column
                label="描述"
                align="center"
                prop="description"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="失败原因"
                align="center"
                prop="fail_reason"
                :show-overflow-tooltip="true"
            />
            <el-table-column
                label="创建时间"
                align="center"
                prop="create_time"
                width="180"
            />
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
  </div>
</template>

<script>
import crud from "@/mixins/crud";
import { http } from "@/api";
import {CommonUserLevelEnum} from "@/utils/enum";

export default {
  name: "index",
  mixins: [crud],
  data() {
    return {
      request: http.balanceRecord,
      searchList: [
        {
          label: '用户类型',
          prop: 'user_type',
          name: 'select',
          clearable: true,
          options: [
            { label: '普通用户', value: CommonUserLevelEnum.NORMAL_USER },
            { label: '商家', value: CommonUserLevelEnum.BUSINESS },
            { label: '一级分销商', value: CommonUserLevelEnum.DISTRIBUTOR_LEVEL1 },
            { label: '二级分销商', value: CommonUserLevelEnum.DISTRIBUTOR_LEVEL2 },
            { label: '平台', value: CommonUserLevelEnum.ADMIN },
          ]
        },
        {
          label: '类型',
          prop: 'type',
          name: 'select',
          clearable: true,
          options: [
            { label: '分销收入', value: 2 },
            { label: '提现', value: 1 },
          ]
        }
      ],
      searchParams: {
        user_type: '',
        type: 2,
      },
      defaultParams: {
        type: 2
      },
      tableData: []
    }
  },
  methods: {
    getUserTypeTag(type) {
      switch (type) {
        case CommonUserLevelEnum.NORMAL_USER:
          return 'info';
        case CommonUserLevelEnum.BUSINESS:
          return 'warning';
        case CommonUserLevelEnum.DISTRIBUTOR_LEVEL1:
          return 'success';
        case CommonUserLevelEnum.DISTRIBUTOR_LEVEL2:
          return 'primary';
        default:
          return 'info';
      }
    },
    getUserTypeLabel(type) {
      switch (type) {
        case CommonUserLevelEnum.NORMAL_USER:
          return '普通用户';
        case CommonUserLevelEnum.BUSINESS:
          return '商家';
        case CommonUserLevelEnum.DISTRIBUTOR_LEVEL1:
          return '一级分销商';
        case CommonUserLevelEnum.DISTRIBUTOR_LEVEL2:
          return '二级分销商';
        case CommonUserLevelEnum.ADMIN:
          return '平台';
        default:
          return '未知';
      }
    }
  }
}
</script>

<style scoped lang="scss">
.income {
  color: #67C23A;
}
.expense {
  color: #F56C6C;
}
</style>
